<template>
  <div ref="contentRef" class="blog-content">
    <div style="display: flex; justify-content: space-between">
      <div class="tags-wrap">
        <TagChip
          v-for="(label, index) in blog.labels"
          :index="index"
          :label="label"
          :show-remove="false"
          @click="router.push('/archive/' + label)" />
      </div>
      <span class="canlender" v-if="blog.created_at"
        ><i class="far fa-calendar"></i>{{ blog.created_at.split("T")[0] }}</span
      >
    </div>
    <div class="hr-line"></div>
    <h1>{{ blog.title }}</h1>
    <MdPreview :modelValue="blog.context" class="content" />
  </div>
</template>

<script setup>
import { MdPreview } from "md-editor-v3"
import { onMounted, onUnmounted, ref } from "vue"
import { useRouter } from "vue-router"
import "md-editor-v3/lib/preview.css"
import TagChip from "@/components/TagChip.vue"

const props = defineProps({
  blog: Object,
})
const emits = defineEmits(["updateMenu"])
const router = useRouter()
const contentRef = ref(null)

// 监听滚动事件，更新当前活动标题
const handleScroll = () => {
  if (!contentRef.value) return

  const headers = contentRef.value.querySelectorAll("h2, h3")
  let currentActiveId = ""
  let currentParentId = ""

  for (let i = headers.length - 1; i >= 0; i--) {
    const header = headers[i]
    const rect = header.getBoundingClientRect()

    if (rect.top <= 200) {
      if (currentActiveId !== header.id) {
        currentActiveId = header.id
        const activeElement = document.getElementById(currentActiveId)
        if (activeElement) {
          if (activeElement.tagName === "H3") {
            let parentHeader = activeElement.previousElementSibling
            while (parentHeader && parentHeader.tagName !== "H2") {
              parentHeader = parentHeader.previousElementSibling
            }
            if (parentHeader && parentHeader.tagName === "H2") {
              currentParentId = parentHeader.id
            }
          }
          emits("updateMenu", currentActiveId, currentParentId)
        }
      }
      break
    }
  }
}

onMounted(() => {
  setTimeout(() => {
    window.addEventListener("scroll", handleScroll, { passive: true })
  }, 100)
})

onUnmounted(() => {
  window.removeEventListener("scroll", handleScroll, { passive: true })
  contentRef.value = null
})
</script>

<style scoped>
@import "/src/assets/styles/allStyles/previewTheme.css";
.blog-content {
  display: grid;
  grid-template-columns: 1fr;
  background: var(--card-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--glass-border);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  transition: var(--transition);
  padding: 0 40px 40px 40px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.blog-content:hover {
  transform: translateY(-5px);
  box-shadow: var(--shadow);
  border-color: rgba(var(--primary-rgb), 0.5);
}

.content {
  background: var(--card-bg);
  /* padding: 20px; */
}

.canlender {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 40px;
  padding: 10px;
  background: var(--primary-light);
  color: var(--text);
  border-radius: 30px;
  font-size: 14px;
  transition: var(--transition);
}

.tags-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  padding: 40px 0 0 0;
}

.hr-line {
  margin: 30px 0 0 0;
  opacity: 0.3;
  height: 4px;
  background-image: linear-gradient(to right, #ff0000 50%, transparent 0%);
  background-size: 50px 4px;
  background-repeat: repeat-x;
  filter: drop-shadow(0px 0px 2px #ff5252);
  transition: opacity 0.3s, color 0.3s;
}
</style>
